﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;

namespace AutoTestingFramework.Core.Controls
{
    public class CellWebElement:IWebElement
    {
        IWebElement m_Cell;

        public IWebElement Cell
        {
            get { return m_Cell; }
            set { m_Cell = value; }
        }
        
        public int ImmediateChildCount
        {
            get
            {
                return FindElements(By.XPath("./*")).Count;
            }
        }


        #region Base Methods, Not changed
        public void Clear()
        {
            m_Cell.Clear();
        }

        public void Click()
        {
            m_Cell.Click();
        }

        public bool Displayed
        {
            get { return m_Cell.Displayed; }
        }

        public bool Enabled
        {
            get { return m_Cell.Enabled; }
        }

        public string GetAttribute(string attributeName)
        {
            return m_Cell.GetAttribute(attributeName);
        }

        public string GetCssValue(string propertyName)
        {
            return m_Cell.GetAttribute(propertyName);
        }

        public System.Drawing.Point Location
        {
            get { return m_Cell.Location; }
        }

        public bool Selected
        {
            get { return m_Cell.Selected; }
        }

        public void SendKeys(string text)
        {
            m_Cell.SendKeys(text);
        }

        public System.Drawing.Size Size
        {
            get { return m_Cell.Size; }
        }

        public void Submit()
        {
            m_Cell.Submit();
        }

        public string TagName
        {
            get { return m_Cell.TagName; }
        }

        public string Text
        {
            get { return m_Cell.Text; }
        }

        public IWebElement FindElement(By by)
        {
            return m_Cell.FindElement(by);
        }

        public System.Collections.ObjectModel.ReadOnlyCollection<IWebElement> FindElements(By by)
        {
            return m_Cell.FindElements(by);
        }
        #endregion

        public CellWebElement(IWebElement Cell)
        {
            if (Cell.TagName.ToUpper() != "TD" && Cell.TagName.ToUpper() != "TH")
                throw new Exception("Invalid Element '" + Cell.TagName + "'. Only td/th element can be initialized");
            m_Cell = Cell;
        }
       
        /// <summary>
        /// Returns element at a particular index in the immediately childs of the cell
        /// </summary>
        /// <param name="Index">1 based index location</param>
        /// <returns></returns>
        public IWebElement GetChildElement(int Index)
        {
            int childCount=ImmediateChildCount;
            if (Index > ImmediateChildCount)
            {
                throw new Exception("Index:" + Index.ToString() + " is greater than available elements count(" + childCount.ToString() + ")");
            }
            else
                return FindElement(By.XPath("./*[" + Index.ToString() + "]")); 

        }

    }
}
